﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using LeapLibrary;
using Leap;

namespace TronLeapRacing
{
    /*
     * 
     * 
     */

    class MainMenue
    {
        //Timerzeit
        const float TIMER = 2;

        //Contentmanager
        ContentManager Content;

        //Texturen (Buttons, Cursor, Background)
        Texture2D background;
        Texture2D startButton;
        Texture2D optionButton;
        Texture2D exitButton;
        Texture2D cursor;
        Texture2D info;

        //Timer für Fuellanimation
        float timerStart= TIMER;
        float timerOptions = TIMER;
        float timerExit = TIMER;

        //Leap Koordinaten
        Vector2 handPosition;

        //Hintergrundbild position
        Vector2 bgPosition = new Vector2(0, 0);

        //Game Infos
        Game1 g1ref;

        MouseHandler mouse;

        //Konstruktor
        public MainMenue(Game1 g1ref)
        {
            this.g1ref = g1ref;

            mouse = new MouseHandler();
        }

        //Texturen werden aus der Conetentpipeline geladen
        public void LoadContent(ContentManager Content)
        {
            this.Content = Content;
            background = Content.Load<Texture2D>("Background/TronBackground");
            startButton = Content.Load<Texture2D>("Button/tronStart");
            optionButton = Content.Load<Texture2D>("Button/tronOptionen");
            exitButton = Content.Load<Texture2D>("Button/tronBeenden");
            cursor = Content.Load<Texture2D>("Texturen/cursor");
            info = Content.Load<Texture2D>("Texturen/InfoMain");
        }

        //Position der Hand wird abgefragt und Fullanimation wird geladen
        public void Update(Vector2 handPosition, GameTime gameTime)
        {
            Rectangle areaPlay = new Rectangle(300, 150, 192, 67);
            Rectangle areaOption = new Rectangle(300, 250, 192, 67);
            Rectangle areaExit = new Rectangle(300, 350, 192, 67);
            mouse.Update();
            this.handPosition = handPosition;

            // überprüfe ob cursor über Textur ist
            //if (areaPlay.Contains((int)mouse.mouseState.X, (int)mouse.mouseState.Y))
            if (handPosition.X >- 108 && handPosition.X < 115 && handPosition.Y >300 && handPosition.Y <380)
            {
                //Timer zaehlt runter
                timerStart -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (timerStart < 2)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover1");

                if (timerStart < 1.8)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover2");

                if (timerStart < 1.6)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover3");

                if (timerStart < 1.4)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover4");

                if (timerStart < 1.2)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover5");

                if (timerStart < 1)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover6");

                if (timerStart < 0.8)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover7");

                if (timerStart < 0.6)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover8");

                if (timerStart < 0.4)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover9");

                if (timerStart < 0.2)
                    startButton = Content.Load<Texture2D>("Hover/tronStartHover10");

                if (timerStart < 0)
                {
                    //Timer wird resetet
                    timerStart = TIMER;
                    //Gamestate wird auf play gesetzt
                    if (g1ref.getTutorialOn())
                        g1ref.setGamestate("tutorial");
                    else
                        g1ref.setGamestate("play");
                }
            }
            else
            {
                timerStart = TIMER;
                startButton = Content.Load<Texture2D>("Button/tronStart");
            }

                if (handPosition.X > -160 && handPosition.X < 165 && handPosition.Y > 200 && handPosition.Y < 280)
                {
                    timerOptions -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (timerOptions < 2)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover1");

                    if (timerOptions < 1.8)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover2");

                    if (timerOptions < 1.6)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover3");

                    if (timerOptions < 1.4)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover4");

                    if (timerOptions < 1.2)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover5");

                    if (timerOptions < 1)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover6");

                    if (timerOptions < 0.8)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover7");

                    if (timerOptions < 0.6)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover8");

                    if (timerOptions < 0.4)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover9");

                    if (timerOptions < 0.2)
                        optionButton = Content.Load<Texture2D>("Hover/tronOptionenHover10");

                    if (timerOptions < 0)
                    {
                        timerOptions = 5;
                        //gameplay auf options setzen
                        g1ref.setGamestate("option");
                    }
                }

                else
                {
                    timerOptions = TIMER;
                    optionButton = Content.Load<Texture2D>("Button/tronOptionen");
                }

                if (handPosition.X > -150 && handPosition.X < 150 && handPosition.Y > 100 && handPosition.Y < 180)
                {
                    //Zeit zaehlt runter
                    timerExit -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (timerExit < 2)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover1");

                    if (timerExit < 1.8)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover2");

                    if (timerExit < 1.6)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover3");

                    if (timerExit < 1.4)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover4");

                    if (timerExit < 1.2)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover5");

                    if (timerExit < 1)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover6");

                    if (timerExit < 0.8)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover7");

                    if (timerExit < 0.6)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover8");

                    if (timerExit < 0.4)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover9");

                    if (timerExit < 0.2)
                        exitButton = Content.Load<Texture2D>("Hover/tronBeendenHover10");

                    if (timerExit < 0)
                    {
                        //Spiel wird beendet
                        g1ref.Exit();
                    }
                }
                else
                {
                    //Timer wird zurueckgesetzt
                    timerExit = TIMER;
                    exitButton = Content.Load<Texture2D>("Button/tronBeenden");
                }

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, bgPosition, Color.White);
            spriteBatch.Draw(startButton, new Vector2(380, 170), Color.White);
            spriteBatch.Draw(optionButton, new Vector2(330, 270), Color.White);
            spriteBatch.Draw(exitButton, new Vector2(350, 370), Color.White);
            spriteBatch.Draw(info, new Vector2(0, 0), Color.White);
            //Zeichnet Cursor an den Leap-Koordinaten
            spriteBatch.Draw(cursor, new Vector2(handPosition.X +470, -1 *(handPosition.Y-500)), Color.White);
            spriteBatch.End();
        }
    }
}
